
package ltd.letao.mall.service.impl;

import ltd.letao.mall.common.Constants;
import ltd.letao.mall.common.ServiceResultEnum;
import ltd.letao.mall.controller.vo.LeTaoMallShoppingCartItemVO;
import ltd.letao.mall.dao.LeTaoMallGoodsMapper;
import ltd.letao.mall.dao.LeTaoMallShoppingCartItemMapper;
import ltd.letao.mall.entity.LeTaoMallGoods;
import ltd.letao.mall.entity.LeTaoMallShoppingCartItem;
import ltd.letao.mall.util.BeanUtil;
import ltd.letao.mall.service.LeTaoMallShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class LeTaoMallShoppingCartServiceImpl implements LeTaoMallShoppingCartService {

    @Autowired
    private LeTaoMallShoppingCartItemMapper leTaoMallShoppingCartItemMapper;

    @Autowired
    private LeTaoMallGoodsMapper leTaoMallGoodsMapper;

    @Override
    public String saveLeTaoMallCartItem(LeTaoMallShoppingCartItem leTaoMallShoppingCartItem) {
        LeTaoMallShoppingCartItem temp = leTaoMallShoppingCartItemMapper.selectByUserIdAndGoodsId(leTaoMallShoppingCartItem.getUserId(), leTaoMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            temp.setGoodsCount(leTaoMallShoppingCartItem.getGoodsCount());
            return updateLeTaoMallCartItem(temp);
        }
        LeTaoMallGoods leTaoMallGoods = leTaoMallGoodsMapper.selectByPrimaryKey(leTaoMallShoppingCartItem.getGoodsId());
        //商品为空
        if (leTaoMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = leTaoMallShoppingCartItemMapper.selectCountByUserId(leTaoMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (leTaoMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (leTaoMallShoppingCartItemMapper.insertSelective(leTaoMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateLeTaoMallCartItem(LeTaoMallShoppingCartItem leTaoMallShoppingCartItem) {
        LeTaoMallShoppingCartItem leTaoMallShoppingCartItemUpdate = leTaoMallShoppingCartItemMapper.selectByPrimaryKey(leTaoMallShoppingCartItem.getCartItemId());
        if (leTaoMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (leTaoMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //当前登录账号的userId与待修改的cartItem中userId不同，返回错误
        if (!leTaoMallShoppingCartItemUpdate.getUserId().equals(leTaoMallShoppingCartItem.getUserId())) {
            return ServiceResultEnum.NO_PERMISSION_ERROR.getResult();
        }
        //数值相同，则不执行数据操作
        if (leTaoMallShoppingCartItem.getGoodsCount().equals(leTaoMallShoppingCartItemUpdate.getGoodsCount())) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        leTaoMallShoppingCartItemUpdate.setGoodsCount(leTaoMallShoppingCartItem.getGoodsCount());
        leTaoMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (leTaoMallShoppingCartItemMapper.updateByPrimaryKeySelective(leTaoMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public LeTaoMallShoppingCartItem getLeTaoMallCartItemById(Long leTaoMallShoppingCartItemId) {
        return leTaoMallShoppingCartItemMapper.selectByPrimaryKey(leTaoMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId, Long userId) {
        LeTaoMallShoppingCartItem leTaoMallShoppingCartItem = leTaoMallShoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemId);
        if (leTaoMallShoppingCartItem == null) {
            return false;
        }
        //userId不同不能删除
        if (!userId.equals(leTaoMallShoppingCartItem.getUserId())) {
            return false;
        }
        return leTaoMallShoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemId) > 0;
    }

    @Override
    public List<LeTaoMallShoppingCartItemVO> getMyShoppingCartItems(Long leTaoMallUserId) {
        List<LeTaoMallShoppingCartItemVO> leTaoMallShoppingCartItemVOS = new ArrayList<>();
        List<LeTaoMallShoppingCartItem> leTaoMallShoppingCartItems = leTaoMallShoppingCartItemMapper.selectByUserId(leTaoMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(leTaoMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> leTaoMallGoodsIds = leTaoMallShoppingCartItems.stream().map(LeTaoMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<LeTaoMallGoods> leTaoMallGoods = leTaoMallGoodsMapper.selectByPrimaryKeys(leTaoMallGoodsIds);
            Map<Long, LeTaoMallGoods> leTaoMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(leTaoMallGoods)) {
                leTaoMallGoodsMap = leTaoMallGoods.stream().collect(Collectors.toMap(LeTaoMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (LeTaoMallShoppingCartItem leTaoMallShoppingCartItem : leTaoMallShoppingCartItems) {
                LeTaoMallShoppingCartItemVO leTaoMallShoppingCartItemVO = new LeTaoMallShoppingCartItemVO();
                BeanUtil.copyProperties(leTaoMallShoppingCartItem, leTaoMallShoppingCartItemVO);
                if (leTaoMallGoodsMap.containsKey(leTaoMallShoppingCartItem.getGoodsId())) {
                    LeTaoMallGoods leTaoMallGoodsTemp = leTaoMallGoodsMap.get(leTaoMallShoppingCartItem.getGoodsId());
                    leTaoMallShoppingCartItemVO.setGoodsCoverImg(leTaoMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = leTaoMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    leTaoMallShoppingCartItemVO.setGoodsName(goodsName);
                    leTaoMallShoppingCartItemVO.setSellingPrice(leTaoMallGoodsTemp.getSellingPrice());
                    leTaoMallShoppingCartItemVOS.add(leTaoMallShoppingCartItemVO);
                }
            }
        }
        return leTaoMallShoppingCartItemVOS;
    }
}
